<?php

namespace core\libs;

class http {
    /*	
	 * 读取远程URL（http,https）	 
	 * @param $url    远程URL 
	 * @param $data   数组
	 * @param @return 返回内容	 
	 */	
    public static function curl($url, $data = null, $header = null) {
        $curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if(!empty($header)){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            curl_setopt($curl, CURLOPT_HEADER, 0);//返回response头部信息
        }
		
		if (!empty($data)) {
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		
		return $output;
    }

    /** 
     * 多线程获取远程数据
     *  
     * @param array $nodes    URL和参数信息。
	 * @param int   $timeOut  超时设置 默认0秒, 为不限制 
	 * 
	 * 		$nodes = array( 
     *              [0] = > array( 
     *              	'url' => 'http://www.baidu.com', 
     *              	'data' => '{"a":1,"b":2}' 
     *              ), 
     *              .... 
     * 		) 
	 * 
     * @return array   
     */  
    public static function curlMulti($nodes, $timeOut = 0) {
        try {  
            if (false == is_array($nodes)) {  
                return array();  
            }  
  
            $mh = curl_multi_init();   
            $curlArray = array();  
            foreach($nodes as $key => $info) {  
                if(false == is_array($info)) {  
                    continue;  
                }  
                if(false == isset($info['url'])) {  
                    continue;  
                }  
  
                $ch = curl_init();  
                // 设置url  
                $url = $info['url'];  
                curl_setopt($ch, CURLOPT_URL, $url);  
  
                $data = isset($info['data']) ? $info['data'] :null;  
                if(false == empty($data)) {  
                    curl_setopt($ch, CURLOPT_POST, 1);   
                    // array  
                    if (is_array($data) && count($data) > 0) {  
                        curl_setopt($ch, CURLOPT_POST, count($data));
                    }  
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
                }  
  
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
                // 如果成功只将结果返回，不自动输出返回的内容  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
                // user-agent  
                curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");  
                // 连接服务器超时  
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut); 
				// 下载数据超时
				curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut); 
                  
                $curlArray[$key] = $ch;  
                curl_multi_add_handle($mh, $curlArray[$key]);   
            }  
			
            $running = NULL;   
            do {
                usleep(10000);
                curl_multi_exec($mh, $running);   
            } while($running > 0);   
			
            $res = array();   
            foreach($nodes as $key => $info) {   
                $res[$key] = curl_multi_getcontent($curlArray[$key]);   
            }   
            foreach($nodes as $key => $info) {   
                curl_multi_remove_handle($mh, $curlArray[$key]);   
            }   
            curl_multi_close($mh);       
            return $res;  
        }   
        catch ( Exception $e ) {  
            return array();  
        }  
  
        return array(); 
    }
}